import { useLangStore } from '@/store';

type TargetContext = '_self' | '_parent' | '_blank' | '_top';

export const openWindow = (
  url: string,
  opts?: { target?: TargetContext; [key: string]: any }
) => {
  const { target = '_blank', ...others } = opts || {};
  window.open(
    url,
    target,
    Object.entries(others)
      .reduce((preValue: string[], curValue) => {
        const [key, value] = curValue;
        return [...preValue, `${key}=${value}`];
      }, [])
      .join(',')
  );
};

export const regexUrl = new RegExp(
  '^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
  'i'
);

// 初始化语言配置
export const InitLang = () => {
  let langEnv = localStorage.getItem('lang_env');
  if (!langEnv) {
    langEnv = 'zh-CN';
    localStorage.setItem('lang_env', 'zh-CN');
  }
  const langStore = useLangStore();
  return langStore.getLanguages(langEnv);
};

// 将fullPath中的/转换为.连接
export const slashToDot = (str: string) => {
  if (str) {
    return str.replace(/\/+/g, '.');
  }
  return str;
};

// 将fullPath中的.转换为/连接
export const dotToSlash = (str: string) => {
  if (str) {
    return str.replace(/\.+/g, '/');
  }
  return str;
};

export default null;
